package com.sanji.sjmallmodule.goods;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sanji.sjmallapp.BaseActivity;
import com.sanji.sjmallapp.R;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import butterknife.BindView;
import butterknife.ButterKnife;

/****
 * 图文详情页
 *****/
public class GoodsGraphicActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.common_left_tv)
    TextView commonLeftTv;
    @BindView(R.id.common_right_img)
    ImageView commonRightImg;
    @BindView(R.id.common_back_img)
    ImageView commonBackImg;
    @BindView(R.id.graphic_webview)
    WebView graphicWebView;
    @BindView(R.id.graphic_progressBar)
    ProgressBar graphicProgressBar;
    private String graphicDetails;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_graphic);
        ButterKnife.bind(this);
        initView();

    }

    public void initView() {
        commonLeftTv.setText(getString(R.string.goods_grapic));
        commonBackImg.setOnClickListener(this);
        graphicDetails = getIntent().getStringExtra("graphicDetails");
        graphicWebView.getSettings().setJavaScriptEnabled(true);// 响应JS控件
        graphicWebView.loadDataWithBaseURL(null, getNewContent(graphicDetails), "text/html", "utf-8", null);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back_img:
                finish();
                break;
        }
    }

    private String getNewContent(String htmlText) {
        Document doc = Jsoup.parse(htmlText);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width", "100%").attr("height", "auto");
        }
        return doc.toString();
    }
}
